package pdf;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class SecondForm {
		JFrame frame = new JFrame("MWAB#");
		JTextField p1 = new JTextField(26);
		JButton b1 = new JButton("Next");
	public SecondForm(String file,JFrame mainFrame) {
		frame.setBounds(400, 100, 1000, 800);// Size and Position
		p1.setText("Please input MAWB#");
		p1.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				String userinput =  p1.getText();
				if(userinput.equals("")) {
					p1.setText("Please input MAWB#");
				}
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				String userinput =  p1.getText();
				if(userinput.equals("Please input MAWB#")) {
					p1.setText("");
				}
			}
		});
		
		
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				on(file,mainFrame);
			}
		});
		
		p1.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER) {
					on(file,mainFrame);
				}
				
			}
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});


		frame.setLayout(null);// 
		p1.setBounds(250, 250, 450,30);
		b1.setBounds(710, 250, 100, 30);
		frame.add(p1);
		frame.add(b1);
		frame.setFocusable(true);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void on(String file,JFrame mainFrame){
		String userinput = p1.getText();
		String pattern  = "[0-9]{3}-[0-9]{8}";
		String pattern2 = "[0-9]{3}-[0-9]{8} [A-Z]{3}[0-9]{6}";
		if(!userinput.matches(pattern)&&!userinput.matches(pattern2)) {
			JOptionPane.showMessageDialog(null, "MAWB format invalid. Please type again.", "error", JOptionPane.ERROR_MESSAGE);
			p1.setText("");
		}else {
			int a = SplitPDF2.SplitPdf(file,userinput);
			if(a==0){
				JOptionPane.showMessageDialog(null, "Success!", "Success!", JOptionPane.OK_CANCEL_OPTION);
			}
			else {
				if(a==1) {
					if (JOptionPane.showConfirmDialog(null, "PP/ORD SHIPMENT?", "PP/ORD", JOptionPane.YES_NO_OPTION) == 1)
						JOptionPane.showMessageDialog(null, "DO missing", "Error", JOptionPane.OK_CANCEL_OPTION);
					String message = SplitPDF2.fileMiss(file,userinput);
					JOptionPane.showMessageDialog(null, message, message, JOptionPane.OK_CANCEL_OPTION);
				}
				else if(a==2){
					if (JOptionPane.showConfirmDialog(null, "PP/ORD SHIPMENT?", "PP/ORD", JOptionPane.YES_NO_OPTION) == 1)
						JOptionPane.showMessageDialog(null, "ATME missing", "Error", JOptionPane.OK_CANCEL_OPTION);
					String message = SplitPDF2.fileMiss(file,userinput);
					JOptionPane.showMessageDialog(null, message, message, JOptionPane.OK_CANCEL_OPTION);
				}
				else if(a==3)
					JOptionPane.showMessageDialog(null, "3461 missing", "Error", JOptionPane.OK_CANCEL_OPTION);
			}
			frame.setVisible(false);
			mainFrame.setVisible(true);
		}
	}
}
